package com.david.mydemo.ctl_mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.david.mydemo.ctl_mybatis_plus.dao.ProjectDao;
import com.david.mydemo.ctl_mybatis_plus.model.ProjectModel;
import com.david.mydemo.ctl_mybatis_plus.model.ProjectSearchModel;
import com.david.mydemo.ctl_mybatis_plus.service.IProjectService;
import com.david.mydemo.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("ProjectServiceImpl")
@Slf4j
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectModel> implements IProjectService {

    @Autowired
    private ProjectDao projectDao;

    @Override
    public IPage<ProjectModel> selectProjectPagePlus(ProjectSearchModel model) {
//        if (model.getPageNum() == null || model.getPageNum() < 0) {
//            model.setPageNum(1);
//        }
//        if (model.getPageSize() == null || model.getPageSize() < 0) {
//            model.setPageSize(10);
//        }

        QueryWrapper<ProjectModel> wrapper = new QueryWrapper<>();
        if (model.getId() != null) {
            wrapper.eq("id", model.getId());
        }
        if (!StringUtils.isEmpty(model.getName())) {
            wrapper.like("name", model.getName());
        }
        if (model.getStartTimed() != null) {
            wrapper.ge("create_time", model.getStartTimed());
        }
        if (model.getEndTimed() != null) {
            wrapper.le("create_time", model.getEndTimed());
        }
        Page<ProjectModel> page = new Page<>(model.getPageNum(), model.getPageSize());
        IPage<ProjectModel> selectPage = projectDao.selectPage(page, wrapper);
        return selectPage;
    }

}
